package dynamic;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.Driver;
import dynamic.ibatis.session.DSqlSession;
import dynamic.ibatis.session.SqlSessionFactory;
import dynamic.injector.DMPJSqlInjector;
import dynamic.metadata.EntityClass;
import dynamic.metadata.EntityProperty;
import dynamic.metadata.property.TableField;
import dynamic.mybatisplus.core.DMybatisConfiguration;
import dynamic.mybatisplus.core.toolkit.DGlobalConfigUtils;
import dynamic.mybatisplus.extension.spring.DMybatisSqlSessionFactoryBean;
import dynamic.util.TableInfoReader;
import dynamic.wrapper.DMPJLambdaWrapper;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class Test {

    public static void main(String[] args) throws Exception {


        Map<String,EntityClass> map = TableInfoReader.entityClassMap("com.test");
        EntityClass user = map.get("com.test.User");
        EntityClass address = map.get("com.test.Address");
        EntityClass area = map.get("com.test.Area");
        EntityProperty entityProperty  =  new EntityProperty();
        entityProperty.setPropertyName("address");
        entityProperty.setEntityClass(user);
        TableField field =  new TableField();
        field.exist(false);
        entityProperty.setTableField(field);
        entityProperty.setGenericType(address);
        entityProperty.setPropertyClass(List.class);
        entityProperty.setReadAble(false);
        user.getProperties().put("address",entityProperty);
        DMPJLambdaWrapper mpjLambdaWrapper  =  new DMPJLambdaWrapper(user);


        SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
        DSqlSession sqlSession = sqlSessionFactory.openSession();
        DGlobalConfigUtils.getGlobalConfig(sqlSession.getConfiguration()).setSqlInjector(new DMPJSqlInjector());
        sqlSession.getConfiguration().addMapper(DynamicMapper.class,user);
        sqlSession.getConfiguration().addMapper(DynamicMapper.class,address);
        sqlSession.getConfiguration().addMapper(DynamicMapper.class,area);
        DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class,user);
        DMPJLambdaWrapper dmpjLambdaWrapper =  new DMPJLambdaWrapper(user);
        dmpjLambdaWrapper.selectAll(user).
                selectCollection(address,user.getProperty("address")).
                leftJoin(address,user.getProperty("addressId"),address.getProperty("id"))
        .leftJoin(area,area.getProperty("id"),address.getProperty("areaId"));
        List<?> list  =  mapper.selectJoinList(user,dmpjLambdaWrapper);
        System.out.println(list);
        IPage page =  mapper.selectJoinPage(new Page<>(1, 10),user,dmpjLambdaWrapper);
        System.out.println(page);
    }

    public static DataSource dataSource() throws SQLException {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new Driver());
        dataSource.setUrl("jdbc:mysql://localhost:3306/tt?useSSL=false&characterEncoding=UTF-8");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }


    public static SqlSessionFactory sqlSessionFactory() throws Exception {
        DataSource dataSource = dataSource();
        DMybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new DMybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        mybatisSqlSessionFactoryBean.setConfiguration(new DMybatisConfiguration());
        return mybatisSqlSessionFactoryBean.getObject();
    }


}
